
/*
** server.c
directions to execute
1. compile the program using G++ newserver.c
2. execute the code useing ./a.out
*/

//vector of sockets
// each new client get sockets

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <iostream>
#include <stdio.h>		
#include <sys/types.h>
#include <sys/socket.h> /* Definition of structures needed for sockets */
#include <netinet/in.h>
#include <sys/wait.h>
#include <netinet/in.h>	/* Definition of constants and structures for Internet Domain Addresses */
#include <sys/errno.h>
#include <sys/signal.h>
#include <string.h>
#include <values.h>
 
#define SERV_PORT  65144	/* Default */

		
		
		// int result;
int main()
{
	// creates the main socket so that it can connect to clients

	int sockfd,client_accepter;
	struct sockaddr_in self;
	struct sockaddr_in client_addr;
	//struct sockaddr_storage their_addr; // connector's address information
	socklen_t sin_size;
	char buf[1000];  // ---------------------------------
		
        // error handling
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
	{
		perror("Socket");
		exit(errno);
	}
	
	/*---Initialize address/port structure---*/
	memset((char*)&self,0, sizeof(self));
	self.sin_family = AF_INET;
	self.sin_port = htons(5000);
	self.sin_addr.s_addr = htons(INADDR_ANY);
	
	int optval=1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &optval, sizeof optval);
	/*---Assign a port number to the socket---*/
	if (bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
	{
		perror("socket--bind");
		exit(errno);
	}

	/*---Make it a "listening socket"---*/
	if (listen(sockfd, 1) < 0 )
	{
		perror("socket--listen");
		exit(errno);
	}

    printf("server: waiting for connections...\n");
	int client_addr_length = sizeof(client_addr);
	if ((client_accepter = accept(sockfd, (struct sockaddr*)&client_addr, (socklen_t*)&client_addr_length)) < 0) 
	{
        	perror("could not accept call");
            exit(1);
    }
	//close(sockfd);
	

}


